﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using System.Text;

public partial class adm_surveycomment : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";
    public int totalRes = 0;

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
        if(Request.Params["qid"]!=null)
        {
            a_Survey asv=a_SurveyService.Geta_Survey(int.Parse(Request.Params["qid"].ToString()));
            this.lblQuestion.Text = asv.Question;
        }
    }

    private void _BindRepeater()
    {
        if (Request.Params["qid"] != null)
        {
            int p = 1;
            if (Request.Params["p"] != null)
            {
                p = int.Parse(Request.Params["p"].ToString());
            }
            int totalrec=0;
            a_Surveys_CommentCollection arr = a_Surveys_CommentService.Geta_Surveys_CommentList(a_Surveys_CommentColumns.Comment, ThoiTrang.Data.OrderDirection.ASC, p,50,out totalrec);
            this.rptSurvey.DataSource = arr;
            this.rptSurvey.DataBind();
            int nopage = 1;
            if (totalrec % 50 == 0)
            {
                nopage = totalrec / 50;
            }
            else
            {
                nopage = totalrec / 50 + 1;
            }
            this.ltrPaging.Text = CommonFunc.QueryPaging(p, nopage, 5);
            this.ltrTotalpage.Text = "Page " + p.ToString() + " | " + " Total records :" + totalrec.ToString();
        }
    }

    private void _ReadParam()
    {
        try
        {
            if (Request.Params["title"] != null)
            {
                this.lblGretting.Text = Request.Params["title"];
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    public string Paging(int totalRecord, int recPerPage, int pageSet)
    {
        string qid ="";
        if (Request.Params["qid"] != null)
        {
            qid = Request.Params["qid"].ToString();
        }
        string title = "";
        if (Request.Params["title"] != null)
        {
            title = Request.Params["title"].ToString();
        }

        string currentUrl = HttpContext.Current.Request.Url.ToString();

        int currentPage = HttpContext.Current.Request.QueryString["p"] == null ? 1 : Convert.ToInt32(HttpContext.Current.Request.QueryString["p"]);
        int prePage = currentPage == 1 ? 1 : currentPage - 1;
        int nextPage = currentPage + 1;
        int lastPage = totalRecord / recPerPage + 1;

        string[] parseUrl = currentUrl.Split('?');

        currentUrl = parseUrl[0];
        string currentParam = "";

        if (parseUrl.Length > 1)
        {
            currentParam += "&ckid=" + Request.Params["ckid"].ToString() + "&qid=" + qid +"&title="+title;
            currentParam = currentParam.Trim('&');
        }

        string paging = "<table class='p3 fr'><tr>";
        paging += "<td class='next-pre'><a href='" + currentUrl + "?p=1&" + currentParam + "'><img height='17' width='28' alt='thoi trang' src='../images/nav_previousset_off.gif'/></a></td>";
        paging += "<td class='next-pre'><a href='" + currentUrl + "?p=" + prePage.ToString() + "&" + currentParam + "'><img height='17' width='28' alt='thoi trang' src='../images/nav_previouspage_off.gif'/></a></td>";

        if (currentPage > pageSet)
        {
        }
        else
        {
            for (int i = 1; i < currentPage; i++)
            {
                paging += "<td class='go'><a href='" + currentUrl + "?p=" + i.ToString() + "&" + currentParam + "'>" + i.ToString() + "</a></td>";
            }
        }

        for (int i = currentPage; i < currentPage + pageSet; i++)
        {
            if (i == currentPage)
                paging += "<td class='current'><a href='" + currentUrl + "?p=" + i.ToString() + "&" + currentParam + "'>" + i.ToString() + "</a></td>";
            else
                paging += "<td class='go'><a href='" + currentUrl + "?p=" + i.ToString() + "&" + currentParam + "'>" + i.ToString() + "</a></td>";
        }

        paging += "<td class='next-pre'><a href='" + currentUrl + "?p=" + nextPage.ToString() + "&" + currentParam + "'><img height='17' width='28' alt='thoi trang' src='../images/nav_nextpage_on.gif'/></a></td>";
        paging += "<td class='next-pre'><a href='" + currentUrl + "?p=" + lastPage.ToString() + "&" + currentParam + "'><img height='17' width='28' alt='thoi trang' src='../images/nav_nextset_off.gif'/></a></td>";
        paging += "</tr></table>";

        return paging;
    }
    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._BindRepeater();
            this._ReadParam();
            this._BindGreeting();
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in this.rptSurvey.Items)
        {
            CheckBox chk = (CheckBox)ri.FindControl("chk");
            if (chk != null && chk.Checked == true)
            {
                //Xoa
                Label lblid = (Label)ri.FindControl("lblid");
                a_Surveys_CommentService.Deletea_Surveys_Comment(int.Parse(lblid.Text));
            }
        }
        this._BindRepeater();
        CommonFunc.ShowAlertMessage(this.lblAlert, "Comments deleted sucessfully");
    }
    #endregion 
   
    
}
